package com.example.bigdata.controller;

import com.example.bigdata.mapper.SysUserMapper;
import com.example.bigdata.task.MyTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

/**
 * @author jie
 * @date 2021/4/27 -14:18
 */
@RestController
public class SysUserController {

    @Autowired
    private SysUserMapper sysUserMapper;

    private Timer timer;

    private Map<String, Timer> map = new HashMap<>();

    @GetMapping("/test/{id}")
    public void test(@PathVariable String id) {

        int[] timeout = {1, 2, 8};

        // todo 先间隔时间为0的员工，推微信消息
        System.out.println("推送微信消息");

        if (timeout.length != 0) {
            timer = new Timer();
            for (int i = 0; i < timeout.length; i++) {
                timer.schedule(new MyTask(id, map, i == timeout.length - 1,timeout[i]), timeout[i] * 1000);
            }
            map.put(id,timer);
        }

        /*if (timeout.length!=0){
            timer = new Timer();
            MyTask myTask = new MyTask(timer,id,timeout,map,0);
            timer.schedule(myTask,timeout[0]*1000);

            // todo 如果时间间隔 都是0 不调用
            map.put(id,timer);
        }*/

    }

    @GetMapping("/test2/{id}")
    public void test2(@PathVariable String id) {
        Timer timer = map.get(id);
        System.out.println("进来移除");
        if (null != timer) {
            timer.cancel();
            map.remove(id);
            System.out.println("移除");
        }
    }
}
